package org.bluweb.g4wicket.chart.image;

import java.util.Map;

import org.apache.wicket.markup.ComponentTag;
import org.bluweb.g4wicket.G4WicketImage;


@SuppressWarnings("serial")
public class ChartImage extends G4WicketImage {

	private String chartType;
	private Map<String,Integer> data;
	
	public ChartImage( String id, String chartType, int width, int height, Map<String, Integer> data ) {
		super(id,width,height);
		this.chartType = chartType;
		this.data = data;
	}

	protected void onComponentTag( ComponentTag tag ) {
        super.onComponentTag(tag);
        checkComponentTag(tag, "img");
        
        StringBuilder builder = new StringBuilder( "http://chart.apis.google.com/chart?" );
        builder.append( "cht=" ).append( this.chartType );
        builder.append( "&chs=" ).append( this.getWidth() ).append( 'x' ).append( this.getHeight() );

        StringBuilder d = new StringBuilder( "t:" );
        StringBuilder l = new StringBuilder();
        for( String label : data.keySet() ) {
        	if( l.length() > 0 ) {
        		d.append( ',' );
        		l.append( '|' );
        	}
        	l.append( label );
        	d.append( data.get( label ) );
        }
        
        builder.append( "&chd=" ).append( d.toString() );
        builder.append( "&chl=" ).append( l.toString() );
        
        tag.put( "src", builder.toString() );
    }
}
